/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-01 22:19:07
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.edu.repository;

import com.joysuch.wwyt.edu.bean.EduExamUserDto;
import com.joysuch.wwyt.edu.bean.EduMarkingPapersCenterListBean;
import com.joysuch.wwyt.edu.entity.EduExam;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Tuple;
import java.util.Date;
import java.util.List;

/**
 * EduExamDao
 */
public interface EduExamDao extends JpaRepository<EduExam, Long>, QuerydslPredicateExecutor<EduExam> {
    public Page<EduExam> findAll(Specification<EduExam> spec, Pageable pageable);

    public EduExam save(EduExam bean);

    public void delete(EduExam bean);

    @Query("select count(m) from EduExam m where code = ?1")
    long countOfCode(String code);

    @Query("select count(m) from EduExam m where code = ?1 and id != ?2")
    long countOfCodeExcludeId(String code, Long id);

    @Query("select sum(p.questionCount) from EduExamTestPaper  m, EduTestPaper p where m.testPaperId=p.id and m.examId = ?1")
    public Integer countOfQuestion(Long id);

    @Query("select sum(p.totalScore) as totalScore,sum(p.passScore) as passScore from EduExamTestPaper  m, EduTestPaper p where m.testPaperId=p.id and m.examId = ?1")
    public Tuple sumTotalScoreAndPassScore(Long id);

    @Query("select sum(p.passScore) as passScore from EduExamTestPaper  m, EduTestPaper p where m.testPaperId=p.id and m.examId = ?1")
    public Float sumPassScore(Long id);

    @Query("select name from EduExam m where id = ?1")
    public String getNameById(Long examId);

    @Query("select m from EduExam m where m.state=1")
    List<EduExam> findByState(Integer state);

    List<EduExam> findByStartTimeBetween(Date startTime, Date endTime);

    int countByState(int id);

    @Query(value = "select * from wwyt_edu_exam where START_TIME between  ?1 and ?2 and delete_flag = '0'", nativeQuery = true)
    List<Long> findIdByTime(Date beginTime, Date endTime);

    @Query(value = "select * from wwyt_edu_exam where START_TIME >=  ?1  and delete_flag = '0'", nativeQuery = true)
    List<Long> findIdByBeginTime(Date beginTime);

    @Query(value = "select * from wwyt_edu_exam where START_TIME <=  ?1  and delete_flag = '0'", nativeQuery = true)
    List<Long> findIdByEndTime(Date endTime);

    @Query(value = "select * from wwyt_edu_exam where   delete_flag = '0'", nativeQuery = true)
    List<Long> findAllId();

    @Query("select m.id from EduExam m where code = ?1")
    Long findByCode(String code);

    @Query(value = "select * from wwyt_edu_exam where   delete_flag = '0' AND EXAMINER_USER_IDS is not null  AND if(:#{#params.examName} is null,1=1,NAME like %:#{#params.examName}%)", nativeQuery = true)
    List<EduExam> selectList(@Param("params") EduMarkingPapersCenterListBean.SearchEduMarkingPapersCenter params);

    @Transactional
    @Modifying
    @Query(value = "UPDATE wwyt_edu_exam SET EXAM_MODE = ?2 WHERE ID =?1", nativeQuery = true)
    void updateExamMode(Long examId, Integer examMode);

    @Query(value = "select count(1) from wwyt_edu_exam e  where e.DELETE_FLAG = '0' and e.NAME = ?1",nativeQuery = true)
    Integer selectByName(@Param("examName") String examName);
}
